'use strict';

const Service = require('egg').Service;

class LoginService extends Service {
  async index(user) {
    let { app } = this;
    const isUsername = await app.mysql.select('m_users', {
      where: { username: user.username },
      columns: [ 'username' ],
    });
    if (isUsername.length <= 0) {
      return {
        status: 0,
        msg: '用户不存在'
      };
    } else {
      const isLogin = await app.mysql.select('m_users', {
        where: { username: user.username, password: user.password }
      });
      if (isLogin.length <= 0) {
        return {
          status: 0,
          msg: '密码错误'
        };
      } else {
        return {
          status: 1,
          data: isLogin[0]
        };
      }
    }

  }
}

module.exports = LoginService;
